const createStorage = (prefix = "") => {
    const isBrowser = typeof window !== "undefined" && !!window.localStorage;
  
    return {
      set(key, value, expireInSeconds = null) {
        if (!isBrowser) return;
        const data = {
          value,
          expireAt: expireInSeconds ? Date.now() + expireInSeconds * 1000 : null,
        };
        localStorage.setItem(prefix + key, JSON.stringify(data));
      },
  
      get(key) {
        if (!isBrowser) return null;
        const raw = localStorage.getItem(prefix + key);
        if (!raw) return null;
  
        try {
          const data = JSON.parse(raw);
          if (data.expireAt && Date.now() > data.expireAt) {
            localStorage.removeItem(prefix + key);
            return null;
          }
          return data.value;
        } catch {
          return null;
        }
      },
  
      remove(key) {
        if (isBrowser) localStorage.removeItem(prefix + key);
      },
  
      clear() {
        if (isBrowser) localStorage.clear();
      }
    };
  };
  
  // 创建命名空间 storage
  const appStorage = createStorage("slg_");

export default appStorage;
  